#include "Common.h"
#include "Utility.h"
#include <fstream>
#include <sstream>
#include <windows.h>

//------------------------------------------
bool Utility::LoadFileToString(const std::string& file, std::string& out)
{
	std::ifstream stream(file.c_str(), std::ios::binary);
	if (!stream.good())
		return false;

	std::stringstream buffer;
	buffer << stream.rdbuf();
	out = buffer.str();

	stream.close();
	return true;
}
//------------------------------------------
bool Utility::FindFile(const std::string& file, std::string& fullPath)
{
	fullPath = file;
	if (FileExist(fullPath))
		return true;

	fullPath = CONTENT_PATH + file;
	if (FileExist(fullPath))
		return true;

	return false;
}
//------------------------------------------
bool Utility::FileExist(const std::string& file)
{
	if (GetFileAttributesA(file.c_str()) == 0xffffffff)
		return false;
	return true;
}
//------------------------------------------
std::wstring Utility::ASCIIToWide(const std::string &str)
{
	int size = MultiByteToWideChar( CP_ACP, 0, str.c_str(), str.size(), NULL, 0);
	if( size <= 0 ) 
		return std::wstring();

	std::wstring wstr;
	wstr.resize( size );

	MultiByteToWideChar( CP_ACP, 0, str.c_str(), str.size(), &wstr[0], size );

	return wstr;
}

std::string Utility::WideToASCII(const std::wstring& wide)
{
	int size = WideCharToMultiByte( CP_ACP, 0, wide.c_str(), wide.length(), NULL, 0, NULL, NULL );
	if( size<= 0 ) 
		return std::string();

	std::string dst;
	dst.resize( size );
	WideCharToMultiByte( CP_ACP, 0, wide.c_str(), wide.length(), &dst[0], size, NULL, NULL );

	return dst;
}